package tma.sdc.batch8.mobile10.shareboard.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import tma.sdc.batch8.mobile10.shareboard.dao.StudentDAO;

@Path("/student")
public class StudentServices {

	@GET
	@Path("/search")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getStudent(@QueryParam("username") String username)
			throws Exception {

		String returnString = null;

		try {
			StudentDAO dao = new StudentDAO();

			returnString = dao.queryStudentName(username).toString();

			if ("[]".equals(returnString)) {
				returnString = "Not found username: \"" + username + "\"";
			}

		} catch (Exception e) {
			e.printStackTrace();
			return Response.status(500)
					.entity("Server was not able to process your request")
					.build();
		}

		return Response.ok(returnString).build();
	}

	@GET
	@Path("/login")
	@Produces(MediaType.APPLICATION_JSON)
	public Response login(@QueryParam("username") String username,
			@QueryParam("password") String password) {

		String returnString = null;

		try {
			StudentDAO dao = new StudentDAO();

			returnString = dao.queryStudentLogin(username, password).toString();

		} catch (Exception e) {
			e.printStackTrace();
			return Response.status(500)
					.entity("Server was not able to process your request")
					.build();
		}

		return Response.ok(returnString).build();
	}
}